package cn.bone.ou.community.service.impl;

import cn.bone.base.annotation.MyLogger;
import cn.bone.base.auth.JWTInfo;
import cn.bone.base.bean.CommonResult;
import cn.bone.base.bean.DataGrid;
import cn.bone.base.bean.Ref;
import cn.bone.base.utils.TimeUtil;
import cn.bone.ou.community.bean.Committee;
import cn.bone.ou.community.bean.CommitteeMember;
import cn.bone.ou.community.dto.CommitteeDto;
import cn.bone.ou.community.mapper.CommitteeMapper;
import cn.bone.ou.community.mapper.CommitteeMemberMapper;
import cn.bone.ou.community.param.CommitteeParam;
import cn.bone.ou.community.service.ICommitteeService;
import cn.bone.ou.community.vo.CommitteeVo;
import cn.bone.ou.user.bean.User;
import cn.bone.ou.user.mapper.UserMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class CommitteeServiceImpl implements ICommitteeService {

    @Resource
    private CommitteeMapper committeeMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private CommitteeMemberMapper committeeMemberMapper;

    /***
     * 获取最新的一条业委会信息
     * 如果没有,提示用户需要新创建
     * 如果过期,提示用户新建一个
     */
    public CommonResult<CommitteeVo.GetVo> get(CommitteeParam.GetParam param){

        CommitteeVo.GetVo vo = new CommitteeVo.GetVo();
        Committee committee = committeeMapper.get(param.getCommunityId());
        if(committee==null){
            vo.setState(0);
        }else{
            vo.setCommitteeId(committee.getId());
            vo.setName(committee.getName());
            vo.setNum(committee.getNum());
            vo.setStartDay(TimeUtil.formaterYYYYMMdd(committee.getStartDay()));
            vo.setEndDay(TimeUtil.formaterYYYYMMdd(committee.getEndDay()));
            vo.setContent(committee.getContent());
            vo.setState(committee.getState());
        }
        return Ref.ok(vo);
    }

    @MyLogger(msg = "新增业委会")
    public CommonResult<Void> add(CommitteeParam.AddParam param){
        //小区业委会存在,且没有过期时,不能创建
        Committee committee = committeeMapper.get(param.getCommunityId());
        if(committee!=null && committee.getState()==1){
            return Ref.error("当前业委会可用,不能新建业委会");
        }
        Date startDay = TimeUtil.parseYYYYMMdd(param.getStartDay());
        if(startDay==null){
            return Ref.error("开始日期错误");
        }
        Date endDay = TimeUtil.parseYYYYMMdd(param.getEndDay());
        if(endDay==null){
            return Ref.error("结束日期错误");
        }
        committee = new Committee();
        committee.setCommunityId(param.getCommunityId());
        committee.setName(param.getName());
        committee.setNum(param.getNum());
        committee.setStartDay(startDay);
        committee.setEndDay(endDay);
        committee.setContent(param.getContent());
        committee.setCreateTime(new Date());
        JWTInfo jwtInfo = JWTInfo.get();
        committee.setCreateUserId(jwtInfo.getUserId());
        committee.setState(1);
        committeeMapper.insert(committee);
        return Ref.ok();
    }

    public CommonResult<DataGrid<CommitteeVo.ListMemberVo>> listMember(CommitteeParam.ListMemberParam param){

        List<CommitteeDto.ListMemberDto> dtos = committeeMemberMapper.list(
                param.getCommunityId(), param.getStart(), param.getNum());

        int count = committeeMemberMapper.countList(param.getCommunityId());

        List<CommitteeVo.ListMemberVo> vos = dtos.stream().map(dto->{
            CommitteeVo.ListMemberVo vo = new CommitteeVo.ListMemberVo();
            vo.setCommitteeMemberId(dto.getId());
            vo.setRealName(dto.getRealName());
            vo.setPhoneNum(dto.getPhoneNum());
            vo.setBuildNum(dto.getBuildNum());
            vo.setUnitNum(dto.getUnitNum());
            vo.setHouseNum(dto.getHouseNum());
            vo.setMemberType(dto.getMemberType());
            vo.setCommitteeName(dto.getCommitteeName());
            vo.setCreateTime(TimeUtil.formaterYYYYMMddHHmm(dto.getCreateTime()));
            return vo;
        }).collect(Collectors.toList());

        DataGrid<CommitteeVo.ListMemberVo> dataGrid = new DataGrid<>();
        dataGrid.setRows(vos);
        dataGrid.setTotal(count);
        return Ref.ok(dataGrid);
    }

    @MyLogger(msg = "修改业委会信息")
    public CommonResult modify(CommitteeParam.ModifyParam param){
        Committee committee = committeeMapper.selectById(param.getCommitteeId());
        if(committee==null){
            return Ref.error("错误的业委会编号");
        }
        if(committee.getState()==2){
            return Ref.error("业委会已过期,不能修改");
        }
        Date startDay = TimeUtil.parseYYYYMMdd(param.getStartDay());
        if(startDay==null){
            return Ref.error("开始日期错误");
        }
        Date endDay = TimeUtil.parseYYYYMMdd(param.getEndDay());
        if(endDay==null){
            return Ref.error("结束日期错误");
        }
        committee.setName(param.getName());
        committee.setNum(param.getNum());
        committee.setStartDay(startDay);
        committee.setEndDay(endDay);
        committee.setContent(param.getContent());
        committeeMapper.updateById(committee);
        return Ref.ok();
    }

    @MyLogger(msg = "添加业委会成员")
    public CommonResult addMember(CommitteeParam.AddMemberParam param){
        Committee committee = committeeMapper.get(param.getCommunityId());
        if(committee==null){
            return Ref.error("请先创建业委会");
        }
        if(committee.getState()==2){
            return Ref.error("业委会已过期,请先创建业委会");
        }
        User user = userMapper.getByPhoneNumAndCommunityId(param.getCommunityId(), param.getPhoneNum());
        if(user==null){
            return Ref.error("不存在的手机号");
        }
        CommitteeMember committeeMember = committeeMemberMapper.getByParam(
                param.getCommunityId(), committee.getId(), user.getId());
        if(committeeMember!=null){
            return Ref.error("该成员已存在");
        }
        committeeMember = new CommitteeMember();
        committeeMember.setCommunityId(param.getCommunityId());
        committeeMember.setCommitteeId(committee.getId());
        committeeMember.setUserId(user.getId());
        committeeMember.setMemberType(param.getMemberType());
        committeeMember.setCreateTime(new Date());
        committeeMemberMapper.insert(committeeMember);
        return Ref.ok();
    }

    @MyLogger(msg = "删除业委会成员")
    public CommonResult deleteMember(CommitteeParam.DeleteMemberParam param){
        committeeMemberMapper.deleteById(param.getCommitteeMemberId());
        return Ref.ok();
    }
}
